<template>
  <div class="area-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit('', level)">
          添加
        </el-button>
        <el-button
          v-if="queryForm.ProvinceCode || queryForm.CityCode"
          type="success"
          @click="backUpLevel"
        >
          返回上一级
        </el-button>

        <!-- <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <!-- 省的列表 -->
    <el-table
      v-if="!queryForm.ProvinceCode && !queryForm.CityCode"
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="ProvinceID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="省">
        <template #default="{ row }">
          <el-button text @click="getCityList(row)">
            {{ row.ProvinceName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="辖下园区" width="200">
        <template #default="{ row }">
          <el-button text @click="ParkArea(row, 1)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开放" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.nState"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开启" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row, 1)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 市的列表 -->
    <el-table
      v-if="queryForm.ProvinceCode && !queryForm.CityCode"
      v-loading="listLoading"
      :data="cityList"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="CityID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="市">
        <template #default="{ row }">
          <el-button text @click="getCountyList(row)">
            {{ row.CityName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="辖下园区" width="200">
        <template #default="{ row }">
          <el-button text @click="ParkArea(row, 2)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否开放" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.nState"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开启" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row, 2)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 区域列表 -->
    <el-table
      v-if="queryForm.CityCode"
      v-loading="listLoading"
      :data="countyList"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="CountyID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="区"
        prop="CountyName"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="辖下园区" width="200">
        <template #default="{ row }">
          <el-button text @click="ParkArea(row, 3)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开放" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.nState"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开启" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row, 3)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete, cityList, countyList } from '@/api/area'
  import Edit from './components/AreaEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'Area',
    components: { Edit },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const router = useRouter()
      const state = reactive({
        level: 1,
        countyList: [],
        cityList: [],
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          ProvinceCode: 0,
          CityCode: 0,
          CountyCode: 0,
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row, num) => {
        console.log(num, '222')

        // 省
        if (num == 1) {
          if (row.ProvinceID) {
            state.editRef.showEdit(row, num)
          } else {
            state.editRef.showEdit('', num)
          }
        }
        // 世
        if (num == 2) {
          if (row.CityID) {
            state.editRef.showEdit(row, num)
          } else {
            state.editRef.showEdit('', num)
          }
        }
        // 区
        if (num == 3) {
          if (row.CountyID) {
            state.editRef.showEdit(row, num)
          } else {
            state.editRef.showEdit('', num)
          }
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.level = 1
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })
      // 查看辖下园区
      const ParkArea = (row, num) => {
        if (num == 1) {
          state.queryForm.ProvinceCode = row.ProvinceCode
        }
        if (num == 2) {
          state.queryForm.CityCode = row.CityCode
        }
        if (num == 3) {
          state.queryForm.CountyCode = row.CountyCode
        }
        router.push({
          path: '/community/communityIndex',
          query: {
            ProvinceCode: state.queryForm.ProvinceCode,
            CityCode: state.queryForm.CityCode,
            CountyCode: state.queryForm.CountyCode,
          },
        })
      }
      // 获取城市列表
      const getCityList = async (row) => {
        state.level = 2
        state.listLoading = true
        state.queryForm.ProvinceCode = row.ProvinceCode
        const {
          data: { list, total },
        } = await cityList(state.queryForm)
        state.cityList = list
        state.total = total
        state.listLoading = false
      }

      // 获取区县列表
      const getCountyList = async (row) => {
        state.level = 3
        state.listLoading = true
        state.queryForm.CityCode = row.CityCode
        const {
          data: { list, total },
        } = await countyList(state.queryForm)
        state.countyList = list
        state.total = total
        state.listLoading = false
      }

      // 返回上级
      const backUpLevel = () => {
        if (state.queryForm.CityCode) {
          state.queryForm.CityCode = 0
        } else {
          if (state.queryForm.ProvinceCode) {
            state.queryForm.ProvinceCode = 0
          }
        }
      }

      return {
        ...toRefs(state),
        setSelectRows,
        getCityList,
        getCountyList,
        ParkArea,
        handleEdit,
        backUpLevel,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
